import './assets/main.css'

import { createApp } from 'vue'
import { createPinia } from 'pinia'

import App from './App.vue'
import router from './router'

import { NavBar } from 'vant';
import { Cell, CellGroup } from 'vant';
import { Icon } from 'vant';
import { Divider } from 'vant';
import { Form, Field } from 'vant';
import { Uploader } from 'vant';
import { Tab, Tabs } from 'vant';
import { Toast } from 'vant';

import Antd from 'ant-design-vue';
import 'ant-design-vue/dist/reset.css';

import axios from 'axios';
import VueAxios from 'vue-axios';

import CryptoJS from 'crypto-js'

// import Vconsole from 'vconsole';

const app = createApp(App)

app.use(createPinia())
app.use(router)

app.use(NavBar);
app.use(Cell);
app.use(CellGroup);
app.use(Icon);
app.use(Divider);
app.use(Form);
app.use(Field);
app.use(Uploader);
app.use(Tab);
app.use(Tabs);
app.use(Toast);

app.use(Antd);

// axios.defaults.baseURL = 'https://gateway.ihuanggang.cn/ee6316bf-ba0b-42c1-8c6f-640db684c4bc';
app.use(VueAxios, axios);
app.provide('axios', app.config.globalProperties.axios)  // provide 'axios'

app.mount('#app')

// 全局声明
declare module 'vue' {
    export interface ComponentCustomProperties {
        $functions: Function,

    }
}
type Function = {
    finishForm: () => void
    downloadFile: (event: any, filename: string) => void
    updateUserInfo: (name: any, mobile: any, idCard: any, debugInfo: any) => void
    decrypt: (word: string, key: string) => string
}

// 全局函数
app.config.globalProperties.$functions = {
    finishForm() {
        ihgAppJssdk.operateWindow.zcResult({ "zcbjData": {} })
    },
    downloadFile(event: any, filename: string) {
        event.preventDefault() // 阻止默认的跳转行为
        ihgAppJssdk.network.downloadFile({
            url: event.target.href,
            folderName: '林业局',
            fileName: filename,
            success: function (res: any) {
                console.log(res)
            }
        })
    },
    decrypt(word: string, keyStr: string) {
        const key = CryptoJS.enc.Utf8.parse(keyStr);
        const decrypt = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
        return CryptoJS.enc.Utf8.stringify(decrypt).toString()
    },
    updateUserInfo(name, mobile, idCard, debugInfo) {
        ihgAppJssdk.user.getoToken({
            appId: 'HG_LDJY_FXCYBT',
            success: function (data: any) {
                ihgAppJssdk.network.request({
                    url: 'https://gateway.ihuanggang.cn/ee6316bf-ba0b-42c1-8c6f-640db684c4bc',
                    method: 'post',
                    data: {
                        oToken: data.data
                    },
                    headers: {
                        'Content-Type': 'application/json;charset=utf-8',
                        'x-hg-api-key': 'e11908d7-f0c5-4d81-a27a-4a0b0ea6b502'
                    },
                    success: function (res: { code: number; data: any }) {
                        if (res.code == 1) {
                            let redata = app.config.globalProperties.$functions.decrypt(res.data, "1dddf3a91316460092016a11c5393044");
                            if (name) {
                                name.value = JSON.parse(redata).name
                            }
                            if (mobile) {
                                mobile.value = JSON.parse(redata).mobilePhone
                            }
                            if (idCard) {
                                idCard.value = JSON.parse(redata).idCard
                            }
                        }
                    }
                });
            }
        })
    }
}

// const vConsole = new Vconsole();